package zhongbo.java.air.auth.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import zhongbo.java.air.auth.service.IAuthService;
import zhongbo.java.air.common.constants.AirConstants;
import zhongbo.java.air.common.exception.NoStackLogException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 说明：
 * 认证请求拦截器
 * <p>
 * 拦截请求, 并验证token
 *
 * @author 钟波dellNotebook
 * @since 2025年03月29日 星期六 16:12:16
 */
@Component
@Slf4j
public class AuthInterceptor implements HandlerInterceptor {

    @Autowired
    private IAuthService authService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String token = request.getHeader(AirConstants.authHeader);
        boolean validateToken = authService.validateToken(token);
        if (!validateToken) {
            log.error("token验证失败");
            throw new NoStackLogException("token验证失败");
        }
        return true;
    }

}
